/**
 * phi
 */

var tm = tm || {};

(function(np){
    var KEY_LEFT  = 37;
    var KEY_UP    = 38;
    var KEY_RIGHT = 39;
    var KEY_B     = 66;
    
    var keyboard = [];
    var isKey = function(key){ return keyboard[key]; };
    
    var floor = window.innerHeight - 50;
    
    var MARIO_BITMAP_WIDTH = 14;
    var MARIO_BITMAP_HEIGHT= 18;

    var COLOR_LIST = [
        [0, 0, 0, 0],   // 透明
        [0xdc, 0x29, 0, 0xff],   // 赤
        [0xff, 0xa5, 0x3b, 0xff], // 肌
        [32, 32, 32, 0xff]   // 茶色
    ];
    
    var MARIO_BITMAP = [
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
        0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
        0, 0, 0, 3, 3, 3, 2, 2, 3, 2, 0, 0, 0, 0,
        0, 0, 3, 2, 3, 2, 2, 2, 3, 2, 2, 2, 0, 0,
        0, 0, 3, 2, 3, 3, 2, 2, 2, 3, 2, 2, 2, 0,
        0, 0, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 0, 0,
        0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0,
        0, 0, 0, 3, 3, 1, 3, 3, 3, 0, 0, 0, 0, 0,
        0, 0, 3, 3, 3, 1, 3, 3, 1, 3, 3, 3, 0, 0,
        0, 3, 3, 3, 3, 1, 3, 3, 1, 3, 3, 3, 3, 0,
        0, 2, 2, 3, 1, 2, 1, 1, 2, 1, 3, 2, 2, 0,
        0, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 0,
        0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 0,
        0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0,
        0, 0, 3, 3, 3, 0, 0, 0, 0, 3, 3, 3, 0, 0,
        0, 3, 3, 3, 3, 0, 0, 0, 0, 3, 3, 3, 3, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ];
    
    var createBitmap = function(context, bitmap, colorList, width, height)
    {
        var imageData = context.createImageData(width, height);
        
        for (var i=0; i<width*height; ++i) {
            var colorIndex = bitmap[i];
            var color = colorList[colorIndex];
            imageData.data[i*4 + 0] = color[0];
            imageData.data[i*4 + 1] = color[1];
            imageData.data[i*4 + 2] = color[2];
            imageData.data[i*4 + 3] = color[3];
        }
        
        return imageData;
    };
    
    
    np.Mario = function(param)
    {
        // 定数
        this.SPEED = 5;
        
        var self = this;
        
        param = param || {};
        var scale = param.scale || 2;
        
        var eMario = document.createElement("canvas");
        eMario.width = MARIO_BITMAP_WIDTH  * scale;
        eMario.height= MARIO_BITMAP_HEIGHT * scale;
        document.body.appendChild(eMario);
        eMario.style.position = "fixed";
        eMario.style.left   = "50px";
        eMario.style.top    = "-50px";
        eMario.style.margin = "0";
        eMario.style.padding= "0";
        eMario.style.zIndex = "0";
        
        var floor = window.innerHeight - 50 - eMario.height;
        
        var setBitmap = function(){
            var tempCanvas = document.createElement("canvas");
            tempCanvas.width = MARIO_BITMAP_WIDTH;
            tempCanvas.height= MARIO_BITMAP_HEIGHT;
            var tempContext = tempCanvas.getContext("2d");
            var imageData = createBitmap(tempContext, MARIO_BITMAP, COLOR_LIST, MARIO_BITMAP_WIDTH, MARIO_BITMAP_HEIGHT);
            
            tempContext.putImageData(imageData, 0, 0);
            
            var context = eMario.getContext("2d");
            //context.fillRect(0, 0, eMario.width, eMario.height);
            context.drawImage(tempCanvas, 0, 0, MARIO_BITMAP_WIDTH, MARIO_BITMAP_HEIGHT, 0, 0, eMario.width, eMario.height);
        };
        setBitmap();
        
        
        
        document.addEventListener("keydown", function(e){
            keyboard[e.keyCode] = true;
        });
        document.addEventListener("keyup", function(e){
            keyboard[e.keyCode] = false;
        });
        
        var jump = 0;
        
        setInterval(function(){
            var x = Number( eMario.style.left.replace("px", '') );
            
            var speed = (isKey(KEY_B)) ? 2:1;
            
            if (isKey(KEY_LEFT)) {
                x-=self.SPEED*speed;
                eMario.style.webkitTransform = "scaleX(-1)";
                eMario.style.MozTransform = "scaleX(-1)";
            }
            if (isKey(KEY_RIGHT)) {
                x+=self.SPEED*speed;
                eMario.style.webkitTransform= "scaleX( 1)";
                eMario.style.MozTransform   = "scaleX( 1)";
            }
            
            if (x < 0) { x=window.innerWidth; }
            if (x > window.innerWidth) { x=0; }
            eMario.style.left = x + "px";
            
            var y = Number( eMario.style.top.replace("px", '') );
            
            if (y == floor && isKey(KEY_UP)) jump = -16;
            
            jump += 0.5;
            
            y += jump;
            
            if (y >= floor) {
                y = floor;
                jump = 0;
            }
            
            eMario.style.top = y + "px";
            
            /*
            if (param.block) {
                for (var i=0; i<param.block.length; ++i) {
                    if (isCollision(eMario, param.block[i]) === true) {
                        console.log("abc");
                    }
                }
            }
            */
            
        }, 1000.0/30);
        
        // 高さを更新
        window.addEventListener("resize", function(){
            floor = window.innerHeight - 50 - eMario.height;
        });
        
        eMario.onclick = function() { this.webkitRequestFullScreen(); }
        
    };
    
    /**
     * http://ruinz.qlookblog.net/js-element-position.html
     * http://d.hatena.ne.jp/terurou/20071018/1192637545
     */
    var isCollision = function(lhs, rhs)
    {
        var lhsRect = lhs.getBoundingClientRect();
        var rhsRect = rhs.getBoundingClientRect();
        if (
            lhsRect.left < rhsRect.left && rhsRect.left < lhsRect.left + lhsRect.width &&
            rhsRect.left < lhsRect.left + lhsRect.width && lhsRect.left + lhsRect.width < rhsRect.left + rhsRect.width
            )
            {
                return true;
            }
    };
    
})(tm);